package application
{
	import flash.display.DisplayObjectContainer;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import application.Commands.StreamCommands.StreamCmdCtr;
	import application.Mediatiors.GameStreamMed;
	import application.Mediatiors.PlayBtnMed;
	import application.Mediatiors.ScoreMed;
	import application.Proxies.ScoreReadPxy;
	import application.Proxies.ScoreWritePxy;
	import application.Proxies.StreamReadPxy;
	import application.Proxies.StreamWritePxy;
	
	import org.gof.application.BaseAppFacade;

	import org.gof.application.BaseWriteProxy;
	import org.gof.application.Clock.FrameClock;
	import org.gof.application.RefreshDataCmd;
	import org.gof.application.SetByAppNS;
	import org.gof.application.AppNS;
	
	use namespace AppNS;
	use namespace SetByAppNS;
	public class BallFacade extends BaseAppFacade
	{
		AppNS var scoreWPxy:ScoreWritePxy;
		AppNS var streamWPxy:StreamWritePxy;
		
		public static const SCORE_RPI:int = 0;
		public static const STREAM_RPI:int =  1;
		
		AppNS var scoreMed:ScoreMed;
		AppNS var playBtnMed:PlayBtnMed;
		AppNS var gameStreamMed:GameStreamMed;
		AppNS var refreshDataCmd:RefreshDataCmd;
		
		public var streamCmdCtr:StreamCmdCtr;
		
		[Embed(source="/assets/GenericGameContainer.swf", symbol="GameContainer")]
		private var AssetClass:Class;
		
		////// Singelton.
		private static var instance:BallFacade;
		private static var _isLocked:Boolean = true;
	
	
		public function initialize(rootViewPort:DisplayObjectContainer):void
		{
			super.init(rootViewPort,2);
			trace ("BallFacade.init");
			mainClock = new FrameClock(3,true,rootViewPort as DisplayObject);
			
			streamCmdCtr =  new StreamCmdCtr(this);
		
			var asset:Sprite = new AssetClass();
			rootViewPort.addChild(asset);
			
			scoreWPxy = new ScoreWritePxy();
			scoreWPxy.score = 0;
			registerProxy(BallFacade.SCORE_RPI,ScoreReadPxy,scoreWPxy);
			
			streamWPxy = new StreamWritePxy();
			streamWPxy.isStarted = false;
			registerProxy(BallFacade.STREAM_RPI,StreamReadPxy,streamWPxy);
			
			scoreMed = new ScoreMed();
			scoreMed.data = scoreWPxy;
			scoreMed.viewPort = asset.getChildByName("score_mc") as DisplayObjectContainer; 

			registerMediator(scoreMed,Vector.<int>([BallFacade.SCORE_RPI]));
			
			playBtnMed = new PlayBtnMed();
			playBtnMed.data = streamWPxy;
			playBtnMed.viewPort = asset.getChildByName("playBtn_mc") as DisplayObjectContainer; 
			registerMediator(playBtnMed);
				
			gameStreamMed = new GameStreamMed();
			gameStreamMed.data = scoreWPxy;
			gameStreamMed.viewPort = asset.getChildByName("gamestream_mc") as DisplayObjectContainer;
			
			refreshDataCmd = new RefreshDataCmd();
			refreshDataCmd.application = this;
			
			mainClock.subscribe(refreshDataCmd.execute);
			mainClock.play();
		
		}
		
		public function BallFacade()
		{
			super();
			
		}
	}
}